package com.bluelight.expertsystemmobile.versionupdate.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import support.action.BaseAction;
import support.common.ApkInfo;
import support.entity.Pagination;

@ParentPackage(value = "json-default")
@Namespace("/")
@Action(value = "/fileUpload", results = { @Result(name = "json", type = "json", params = { "contentType", "text/html" }),
		@Result(name = "success", location = "/index.jsp") })
public class FileUploadAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected Pagination pagination = new Pagination();

	private File file;
	private String fileFileName;
	private String fileFileContentType;
	private String fileContentType;

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	private String message = "上传成功";
	private String error = "";
	private long fileSize;

	@JSON
	public long getFileSize() {
		return fileSize;
	}

	@JSON
	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	@JSON
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileFileContentType() {
		return fileFileContentType;
	}

	public void setFileFileContentType(String fileFileContentType) {
		this.fileFileContentType = fileFileContentType;
	}

	@SuppressWarnings("deprecation")
	@Override
	public String execute() throws Exception {
		PropertiesConfiguration p = new PropertiesConfiguration("upload.properties");
		String uploadPath = p.getString("upload.apk.path");
		String path = ServletActionContext.getRequest().getRealPath(uploadPath);

		File folder = new File(path);
		if (!folder.isFile()) {
			folder.mkdir();
		}

		try {
			File f = this.getFile();
			if (!this.getFileFileName().endsWith(".apk")) {
				error = "上传失败，必须是APK文件";
				return JSON;
			}
			FileInputStream inputStream = null;
			FileOutputStream outputStream = null;
			String newFileName = path + File.separator + this.getFileFileName();
			if (f != null) {
				try {
					inputStream = new FileInputStream(f);
					outputStream = new FileOutputStream(newFileName);
					byte[] buf = new byte[1024];
					int length = 0;
					while ((length = inputStream.read(buf)) != -1) {
						outputStream.write(buf, 0, length);
					}
					outputStream.flush();

				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (inputStream != null) {
						inputStream.close();
						inputStream = null;
					}
					if (outputStream != null) {
						outputStream.close();
						outputStream = null;
					}
				}
				fileSize = new File(newFileName).length();
				message = ApkInfo.getVersionCode(newFileName);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "上传异常";
		}
		return JSON;
	}
}
